//
// @Author: zhengqi Wu
// @Description: 表示内容兴趣包中的组件 InterestLifeTime
// @Version: 1.0.0
// @Date: 2021/3/14 13:54
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
//

const encoding = require('../encoding')
var {InitialAble,TlvComponentBase} = require('./TlvComponentBase')
var aggregation = require('../common/aggregation')//实现了多继承

class InterestLifeTime extends aggregation(TlvComponentBase,InitialAble,encoding.IEncodingAble, encoding.SelfEncodingBase){
    constructor() {
        super();
        this.interestLifeTime = BigInt(0) // uint64
    }

     getInterestLifeTime(){
        return this.interestLifeTime
     }

     setInterestLifeTime(interestLifeTime){
        this.interestLifeTime = interestLifeTime
        this.doInitial()
     }

    /**
     * 将 InterestLifeTime 线速编码为一个 TLV
     * @param enc {encoding.encoder.Encoder}
     * @returns {number}
     */
     wireEncode(enc){
         let totalLength = 0
         try{
             // 编码TLV-VALUE
             let tmpLen = enc.prependNonNegativeInteger(this.interestLifeTime)
             // DONE 错误处理
             totalLength += tmpLen

             // 编码TLV-LENGTH
             tmpLen = enc.prependVarNumber((totalLength))
             // DONE 错误处理
             totalLength += tmpLen

             // 编码 TLV-TYPE
             tmpLen = enc.prependVarNumber((encoding.tlv.ComponentType.TlvInterestLifeTime))
             // DONE 错误处理
             totalLength += tmpLen

             return totalLength
         }catch (e){
             throw e
         }

     }

    /**
     * @description 从 TLV Block 中解码出一个 InterestLifeTime 组件
     * @param block {encoding.block.Block}
     */
    wireDecode(block){
        try {
            // 检查 Type 是否正确
            encoding.tlv.expectType(block.getType(), encoding.tlv.ComponentType.TlvInterestLifeTime)

            // 读取 InterestLifeTime
            let value = encoding.tlv.readNonNegativeInteger(block.getValue(), 0, block.getLength())
            this.setInterestLifeTime(BigInt(value))
        } catch (error) {
            throw error
        }
    }
}

module.exports = {
InterestLifeTime:InterestLifeTime
}